import request from '@/utils/request.js';

const state = {
	mainInfo: {},
	skuInfo: [],
};
const mutations = {
	SET_MAININFO(state, mainInfo){
		state.mainInfo = mainInfo;
	},
	SET_SKUINFO(state, skuInfo){
		state.skuInfo = skuInfo;
	},
	APPEND_SKUINFO(state, appendSkuInfo){
		state.skuInfo = state.skuInfo.concat(appendSkuInfo);
	}
};
const actions = {
	async getMainInfo({ commit }) {
		try{
			const result = await request('/getHomeData');
			if(result.msg === "success"){
				commit('SET_MAININFO', result.data);
				commit('SET_SKUINFO', result.data.skuInfo);
			}else{
				uni.showToast({
					title: '获取首页数据失败!'
				});
			}
		}catch(e){
			uni.showToast({
				title: '请求获取首页数据失败!'
			});
		}
	},
	async getAppendSkuInfo({ commit }) {
		try{
			const result = await request('/getHomeAppendData');
			if(result.msg === "success"){
				commit('APPEND_SKUINFO', result.data.skuInfo);
			}else{
				uni.showToast({
					title: '追加获取首页数据失败!'
				});
			}
		}catch(e){
			uni.showToast({
				title: '请求追加获取首页数据失败!'
			});
		}
	}
};
const getters = {
	mainTitleInfo(state) {
		return (state.mainInfo.info || {}).titleConfig || {}
	}
};

export default {
	namespaced: true,
	state,
	mutations,
	actions,
	getters
}